package mpcs.utils
{
	import com.google.maps.InfoWindowOptions;
	import com.google.maps.LatLng;
	import com.google.maps.Map3D;
	import com.google.maps.MapMouseEvent;
	import com.google.maps.MapType;
	import com.google.maps.View;
	import com.google.maps.controls.MapTypeControl;
	import com.google.maps.controls.MapTypeControlOptions;
	import com.google.maps.geom.Attitude;
	import com.google.maps.overlays.Marker;
	import com.google.maps.overlays.MarkerOptions;
	import com.google.maps.services.ClientGeocoder;
	import com.google.maps.services.ClientGeocoderOptions;
	import com.google.maps.services.GeocodingEvent;
	
	import flash.events.Event;
	import flash.geom.Point;
	
	import mpcs.view.skins.MyMapInfoWindow;
	
	/**
	 *  地图控制类
	 * @author zhangzuoqiang
	 */	
	public class MapManager {
		
		public function MapManager() {
		}
		
		public static var googleMap:GoogleMap;
		
		public static var gmap:Map3D;
		
		public static var setmap:Map3D;
		
		/**地图缩放级别**/
		public static var ZOOM:int = 15;
		
		/**
		 * 保存最新的位置坐标 
		 */		
//		public static var freshLatLng:LatLng;
		
		public static var startLat:Number = 22.905955377583084;
		public static var startLng:Number = 113.86926770210266;
		public static var startLatLng:LatLng = new LatLng( startLat, startLng );
		
		private static var currentAddr:String;
		
		[Bindable] public static var setAddr:String = "";
		[Bindable] public static var setLat:String = "";
		[Bindable] public static var setLng:String = "";
		
		private static var countFlag:Boolean = true;
		
		/**
		 * 控制地图转向当前最新位置
		 * bug：显示当前位置时，当前该显示的反而显示的是上一次的位置
		 */
		public static function moveToLocation(latlng:LatLng):void{
			/*if(countFlag){
				currentAddr = "";
				countFlag = false;
			}*/
			latlngToAddr(latlng);
			trace("MapManager ... moveToLocation ... " + setAddr );
			gmap.flyTo(latlng, ZOOM, new Attitude(0, 40, 0), 3);
			var obj:Object = {title: "当前位置" , content: currentAddr};
			var option:InfoWindowOptions = new InfoWindowOptions(obj);
			gmap.closeInfoWindow();
			gmap.openInfoWindow(latlng, option);
			
//			latlngToAddr(latlng);
		}
		
		/**
		 * 设置设置面板的地图属性，并返回获取的地理位置和坐标
		 * @return 
		 */			
		public static function initSetMap():void {
			// 设置初始时刻的地图中心，经纬度
			setmap.setCenter(startLatLng, 12, MapType.NORMAL_MAP_TYPE);
			// 添加鼠标滑轮控制
			setmap.enableContinuousZoom();
			setmap.enableScrollWheelZoom();
			//在地图上显示地图的4种类型，可以随意切换 
			setmap.addControl( new MapTypeControl( new MapTypeControlOptions() ) );
			setmap.viewMode = View.VIEWMODE_PERSPECTIVE;
			//创建可拖动的标记
			markerDrag();
		}
		
		/**
		 * 创建可拖动的标记,并且在标记上提示当前位置和经纬度
		 * 并且返回经纬度、地理位置Object对象，给页面表单赋值 
		 * @return 
		 */		
		public static function markerDrag():void {
			var latStr:String = "";
			var lngStr:String = "";
			// 初始转换第一次的位置
//			latlngToAddr(startLatLng);
			
			var marker:Marker = new Marker(setmap.getCenter(), new MarkerOptions({draggable: true}));
			// 创建自定义提示框
			var customContent:MyMapInfoWindow = new MyMapInfoWindow();
			customContent.addEventListener("close", function(event:Event):void {
				marker.closeInfoWindow();
			});
			
			// 鼠标拖动事件处理
			marker.addEventListener(MapMouseEvent.DRAG_START, function(event:MapMouseEvent):void {
				setmap.closeInfoWindow();
			});
			marker.addEventListener(MapMouseEvent.DRAG_END, function(event:MapMouseEvent):void
			{
				var markerContent:String = marker.getLatLng().toString();
				// 修改公共变量的值
				startLatLng = marker.getLatLng();
				latStr = String(startLatLng.lat());
				lngStr = String(startLatLng.lng());
				
				// 转换当前标注所在的经纬度
				latlngToAddr(startLatLng);
				setLat = latStr;
				setLng = lngStr;
				trace("更新设定的经纬度位置信息：[ setAddr = " + setAddr + " , setLng = " + setLng + " , setLat = " + setLat + " ]");
				
				customContent.lat.htmlText = "<b>纬度:</b>" + latStr;
				customContent.lng.htmlText = "<b>经度:</b>" + lngStr;
				var options:InfoWindowOptions = new InfoWindowOptions({customContent: customContent, 
					customOffset: new Point(0, 10)});
				// 显示InfoWindow
				marker.openInfoWindow(options);
			});
			setmap.addOverlay(marker);
		}
		
		/**
		 * 经纬度转化为地址名称 
		 * @param latlng
		 * @return 
		 */		
		public static function latlngToAddr(latlng:LatLng):void {
			var geo:ClientGeocoder = new ClientGeocoder(new ClientGeocoderOptions({countryCode: 'CN', language: 'zh'}));
			geo.reverseGeocode(latlng);
			var xianArr:Array = [];
			geo.addEventListener(GeocodingEvent.GEOCODING_SUCCESS, function(gevt:GeocodingEvent):void {
				var markArr:Array = gevt.response.placemarks;
				trace("=========================   " + markArr.toString());
				xianArr = String(markArr[0]).split(" @"); //县级
				setAddr = xianArr[0].toString();
			});
			geo.addEventListener(GeocodingEvent.GEOCODING_FAILURE, function(gevt:GeocodingEvent):void
			{
				setAddr = "当前位置未知。";
//				Alert.show("执行latlngToAddr()函数GeocodingEvent.GEOCODING_FAILURE", "当前所处位置为海域");
			});
		}
		
	}
}